package com.icoolkj.mall.product.service.impl;

import com.icoolkj.mall.common.Result;
import com.icoolkj.mall.product.dto.ProductRequest;
import com.icoolkj.mall.product.dto.ProductResponse;
import com.icoolkj.mall.product.mapper.ProductMapper;
import com.icoolkj.mall.product.model.Product;
import com.icoolkj.mall.product.service.ProductService;
import io.seata.core.context.RootContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @author icoolkj
 * @version 1.0
 * @description
 * @createDate 2025/01/16 11:39
 */
@Service
public class ProductServiceImpl implements ProductService {

    Logger logger = LoggerFactory.getLogger(ProductServiceImpl.class);

    @Autowired
    private ProductMapper productMapper;

    public Result<?> createProduct(ProductRequest productRequest){
        Product product = new Product();
        product.setProductName(productRequest.getProductName());
        product.setProductDescription(productRequest.getProductDescription());
        product.setProductPrice(productRequest.getProductPrice());
        product.setCreateTime(new Date());
        productMapper.createProduct(product);
        ProductResponse productResponse = new ProductResponse();
        productResponse.setProductId(product.getProductId());
        productResponse.setProductName(productRequest.getProductName());
        productResponse.setProductDescription(productRequest.getProductDescription());
        productResponse.setProductPrice(productRequest.getProductPrice());
        return Result.ok(productResponse);
    }

    public Result<?> getAllProducts(){
        return Result.ok(productMapper.getAllProducts());
    }

    @Override
    public Result<ProductResponse> getPriceProduct(Long productId) {

        logger.info("[reduceBalance] currenet XID: {}", RootContext.getXID());

        ProductResponse productResponse = productMapper.getPriceProduct(productId);
        if(productResponse == null){
            return Result.fail("productId wrong, please check the productId");
        }
        return Result.ok(productResponse);
    }
}
